************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 20 (Sleep Survey Stagewise Take Up)
* Last edited: 07 May 2021
************************************************

************************************************	
*0. Initial Setup
************************************************

	clear all
	set more off
	pause on			
			
			
************************************************	
*1. Prepping data
************************************************

    use "$d/pop_study_dataset.dta", clear
	
	
	
************************************************	
*2. Summary Tables
************************************************

*** Summary of Averages for Main Study (RCT) and Sleep Survey

    *** Prepare survey stats
	preserve
	
	    collapse (mean) sr_sleep sr_sleep_eff sr_avg_nap total_sr_sleep sr_whether_nap sr_num_nap_day acti_avg_nap acti_nap_eff acti_sleep_eff acti_sleep acti_num_nap_day acti_whether_nap total_sleep, by(pid)
		
		foreach var in sr_sleep sr_sleep_eff sr_avg_nap total_sr_sleep sr_whether_nap sr_num_nap_day acti_avg_nap acti_nap_eff acti_sleep_eff acti_sleep acti_num_nap_day acti_whether_nap total_sleep {
		
		    qui sum `var'
			
			local `var'_m = string(r(mean),"%9.2f")
			local `var'_sd = string(r(sd),"%9.2f")
			
		}
		
	restore
		
	*** Prepare RCT stats
	preserve 
	
	    use "$d/firststage_dataset.dta", clear
		keep if post_treatment == 0 // use baseline data
		
		foreach var in self_report self_report_eff sleep_night sleep_eff {
		
		    qui sum `var'
			
			local `var'_m = string(r(mean),"%9.2f")
			local `var'_sd = string(r(sd),"%9.2f")
			
		}
	
	restore
	

	
*** Sleep Survey Stagewise Take Up

    preserve
	
	    collapse (mean) census baseline interested actigraph_willing installed endline acti_p comply end_m, by(pid) 
		rename actigraph_willing willing
		local demo_vars census baseline interested willing installed endline acti_p comply
		
		distinct pid if end_m != 2
		local all = r(N)
		local laststage = r(N)
		
		foreach var of varlist `demo_vars' {
            
			qui sum  `var'
            local `var'_freq = r(sum)
            local `var'_percent = string((``var'_freq'/`all')*100,"%9.2f")
			local `var'_percent_ls = string((``var'_freq'/`laststage')*100,"%9.2f")
            if `var' != acti_p local laststage = r(sum)
			
        }
		
	restore
	
	*** Stagewise Table

		file open f using "$oa/Tables/TableA20_takeup_stagewise.tex", write replace
			file write f "\begin{tabular}{l*{3}{c}}" _n ///+
			"\toprule" _n ///
			"&\multicolumn{1}{c}{Percent of Last Stage} &\multicolumn{1}{c}{Percent of Total} &\multicolumn{1}{c}{Frequency}   \\" _n ///
			"&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)}  \\" _n ///
			"\midrule" _n ///
			"Census                                                 & `census_percent_ls' & `census_percent' & `census_freq'   \\ [1em]" _n ///
			"Baseline Survey                                        & `baseline_percent_ls' & `baseline_percent' & `baseline_freq'  \\ [1em]" _n ///
			"Interest to Hear about Actigraph                       & `interested_percent_ls' & `interested_percent' & `interested_freq'  \\ [1em]" _n ///
			"Willingness to Wear Actigraph                          & `willing_percent_ls' & `willing_percent' & `willing_freq'  \\ [1em]" _n ///
			"Actigraph Installation                                 & `installed_percent_ls' & `installed_percent' & `installed_freq'  \\ [1em]" _n ///
			"Endline Survey                                         & `endline_percent_ls' & `endline_percent' & `endline_freq'  \\ [1em]" _n ///
			"Actigraph Component Participants (All)                 & `acti_p_percent_ls' & `acti_p_percent' & `acti_p_freq' \\ [1em]" _n ///
			"Actigraph Component Participants (Completed)           & `comply_percent_ls' & `comply_percent' & `comply_freq' \\ [1em]" _n ///
			"\midrule" _n ///
			"\(N\)                                                  & & & `all' \\" _n ///
			"\bottomrule \\" _n ///
			"\end{tabular}" _n
		file close f
		
	
